<?php

require_once 'anomey.inc.php';

// Loads the class
require 'Browscap/Browscap.php';


/**
 * Simplyfied user class with
 * the getNick() method.
 */
class User {
	public function getNick() {
		return 'peter';
	}
}

/**
 * Current user object. This is "null" if the
 * user isn't logged in.
 */
$user = new User();
// $user = null;

/**
 * Simplyfied request class with
 * the important methods for tardo..
 */
class Request {

	private $userAgent;
	private $bc;
	private $browser;
	private $os;

	/**
	 * Session id which rests the same even
	 * if the real session id changes. So it
	 * can be used to track a visit of a user.
	 */
	public function getMeeting() {
		return '2a9c3bc8035c499ffc0217206083167d47deddc9';
	}

	/**
	 * Request method.
	 */
	public function getMethod() {
		return 'GET';
	}

	/**
	 * Trail of the page which the user wants to
	 * see (inside anomey).
	 */
	public function getTrail() {
		return '/home/test/test2';
	}

	/**
	 * User agent string of client.
	 */
	public function getUserAgent() {
		return 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7';
	}

	public function setUserAgent($uA) {
		$this->userAgent = $uA;
	}

	/**
	 * IP or adress of client.
	 */
	public function getRemoteAdress() {
		return '195.176.146.923';
	}

	/**
	 * Browser of Client.
	 */
	public function getBrowser() {
		return $this->browser;
	}

	public function setBrowser($browser) {
		$this->browser = $browser;
	}

	/**
	 * Country of Client.
	 */
	public function getCountry() {
		return 'Switzerlandjjk';
	}

	/**
	 * Operation System of Client.
	 */
	public function getOS() {
		return $this->os;
	}

	public function setOS($os) {
		return $this->os = $os;
	}

	/**
	 * Timstmp of request
	 */

	public function getTimeStmp() {
		return time();
	}

	private $user;

	public function getUser() {
		return $this->user;
	}

	public function __construct($user) {
		$this->user = $user;
		// Creates a new Browscap object (loads or creates the cache)
		$this->bc = new Browscap('Browscap/cache');
		$current_browser = $this->bc->getBrowser();
		$this->setBrowser($current_browser->Parent);
		$this->setOS($current_browser->Platform);
	}
}

$request = new Request($user);

// -----------------------------------------------------------

if($request->getUser() == null) {
	echo 'you aren\' logged in!<br/>'; // anonymous user
} else {
	echo 'welcome '.$request->getUser()->getNick() . '<br/>'; // user is logged in
}


$xml = XML::load('tardo.xml');

echo 'Tardo version: ' . (string) $xml['version'];

foreach($xml->day as $day) {
	echo '<h2>' . (string) $day['date'] . '</h2>';
	foreach($day->browsers->browser as $browser) {
		echo '<p>' . (string) $browser['count'] . ' hits from ' . (string) $browser . '</p>';
	}
	foreach($day->operation_systems->os as $os) {
		echo '<p>' . (string) $os['count'] . ' users use ' . (string) $os . '</p>';
	}
}
//Richtiges tardo!!!
$today = date('Ymd');
echo $today;
$new = true;
//normal stats
foreach($xml->day as $day) {
	$xml_date = (string) $day['date'];

	if($xml_date == $today) {

		$newChild['browser'] = false;
		$newChild['country'] = false;
		$newChild['os'] = false;
		$newChild['trail'] = false;
		$newChild['meeting'] = false;
		$newChild['remoteadress'] = false;


		foreach($day->meetings->meeting as $meeting) {

			if((string) $meeting == $request->getMeeting()) {
				$meeting['count'] +=1;
				$newChild['meeting'] = true;
			}
		}

		foreach($day->browsers->browser as $browser) {

			if((string) $browser == $request->getBrowser()) {
				if(!$newChild['meeting'])
					$browser['count'] +=1;
				$newChild['browser'] = true;
			}
		}

		foreach($day->countries->country as $country) {

			if((string) $country == $request->getCountry()) {
				if(!$newChild['meeting'])
					$country['count'] +=1;
				$newChild['country'] = true;
			}
		}

		foreach($day->operation_systems->os as $os) {

			if((string) $os == $request->getOS()) {
				if(!$newChild['meeting'])
					$os['count'] +=1;
				$newChild['os'] = true;
			}
		}
		foreach($day->trails->trail as $trail) {

			if((string) $trail == $request->getTrail()) {
				if(!$newChild['meeting'])
					$trail['count'] +=1;
				$newChild['trail'] = true;
			}
		}


		foreach($day->remoteadresses->remoteadress as $remoteAdress) {

			if((string) $remoteAdress == $request->getRemoteAdress()) {
				if(!$newChild['meeting'])
					$remoteAdress['count'] +=1;
				$newChild['remoteadress'] = true;
			}
		}
		//New Childs->bsp. new Browser
		foreach($newChild as $key => $value) {

			if(!$value && $key == 'browser') {
				$child = $day->browsers->addChild('browser', $request->getBrowser());
				$child->addAttribute('count', 1);
			} else if(!$value && $key == 'country') {
				$child = $day->countries->addChild('country', $request->getCountry());
				$child->addAttribute('count', 1);
			} else if(!$value && $key == 'os') {
				$child = $day->operation_systems->addChild('os', $request->getOS());
				$child->addAttribute('count', 1);
			} else if(!$value && $key == 'trail') {
				$child = $day->trails->addChild('trail', $request->getTrail());
				$child->addAttribute('count', 1);
			} else if(!$value && $key == 'meeting') {
				$child = $day->meetings->addChild('meeting', $request->getMeeting());
				$child->addAttribute('count', 1);
			} else if(!$value && $key == 'remoteadress') {
				$child = $day->remoteadresses->addChild('remoteadress', $request->getRemoteAdress());
				$child->addAttribute('count', 1);
			}
		}



		$new = false;
		break;
	}

}



if($new) {
	$day = $xml->addChild('day');
	$day['date'] = date('Ymd');

	$browsers = $day->addChild('browsers');
	$browser = $browsers->addChild('browser', $request->getBrowser());
	$browser['count'] = 1;

	$countries = $day->addChild('countries');
	$country = $countries->addChild('country', 'switzerland');
	$country['count'] = 1;

	$oss = $day->addChild('operation_systems');
	$os = $oss->addChild('os', 'Mac OS X Intel');
	$os['count'] = 1;

	$trails = $day->addChild('trails');
	$trail = $trails->addChild('trail', $request->getTrail());
	$trail['count'] = 1;

	$meetings = $day->addChild('meetings');
	$meeting = $meetings->addChild('meeting', $request->getMeeting());
	$meeting['count'] = 1;

	$remoteadresses = $day->addChild('remoteadresses');
	$remoteadress = $remoteadresses->addChild('remoteadress', $request->getRemoteAdress());
	$remoteadress['count'] = 1;

}//end normal stats

//last 10

//number 9 to number 10
$xml->last10->number10->trail 	 = (string)$xml->last10->number9->trail;
$xml->last10->number10->IP 		 = (string)$xml->last10->number9->IP;
$xml->last10->number10->nickname = (string)$xml->last10->number9->nickname;
$xml->last10->number10->timestmp = (string)$xml->last10->number9->timestmp;

//number 8 to number 9
$xml->last10->number9->trail 	= (string)$xml->last10->number8->trail;
$xml->last10->number9->IP 		= (string)$xml->last10->number8->IP;
$xml->last10->number9->nickname = (string)$xml->last10->number8->nickname;
$xml->last10->number9->timestmp = (string)$xml->last10->number8->timestmp;

//number 7 to number 8
$xml->last10->number8->trail 	= (string)$xml->last10->number7->trail;
$xml->last10->number8->IP 		= (string)$xml->last10->number7->IP;
$xml->last10->number8->nickname = (string)$xml->last10->number7->nickname;
$xml->last10->number8->timestmp = (string)$xml->last10->number7->timestmp;

//number 6 to number 7
$xml->last10->number7->trail 	= (string)$xml->last10->number6->trail;
$xml->last10->number7->IP 		= (string)$xml->last10->number6->IP;
$xml->last10->number7->nickname = (string)$xml->last10->number6->nickname;
$xml->last10->number7->timestmp = (string)$xml->last10->number6->timestmp;

//number 5 to number 6
$xml->last10->number6->trail 	= (string)$xml->last10->number5->trail;
$xml->last10->number6->IP		= (string)$xml->last10->number5->IP;
$xml->last10->number6->nickname = (string)$xml->last10->number5->nickname;
$xml->last10->number6->timestmp = (string)$xml->last10->number5->timestmp;

//number 4 to number 5
$xml->last10->number5->trail 	= (string)$xml->last10->number4->trail;
$xml->last10->number5->IP 		= (string)$xml->last10->number4->IP;
$xml->last10->number5->nickname = (string)$xml->last10->number4->nickname;
$xml->last10->number5->timestmp = (string)$xml->last10->number4->timestmp;

//number 3 to number 4
$xml->last10->number4->trail 	= (string)$xml->last10->number3->trail;
$xml->last10->number4->IP 		= (string)$xml->last10->number3->IP;
$xml->last10->number4->nickname = (string)$xml->last10->number3->nickname;
$xml->last10->number4->timestmp = (string)$xml->last10->number3->timestmp;

//number 2 to number 3
$xml->last10->number3->trail 	= (string)$xml->last10->number2->trail;
$xml->last10->number3->IP 		= (string)$xml->last10->number2->IP;
$xml->last10->number3->nickname = (string)$xml->last10->number2->nickname;
$xml->last10->number3->timestmp = (string)$xml->last10->number2->timestmp;

//number 1 to number 2
$xml->last10->number2->trail 	= (string)$xml->last10->number1->trail;
$xml->last10->number2->IP 		= (string)$xml->last10->number1->IP;
$xml->last10->number2->nickname = (string)$xml->last10->number1->nickname;
$xml->last10->number2->timestmp = (string)$xml->last10->number1->timestmp;


//number 1 new datas
$xml->last10->number1->trail	= $request->getTrail();
$xml->last10->number1->IP 		= $request->getRemoteAdress();
$xml->last10->number1->nickname = $request->getUser()->getNick();
$xml->last10->number1->timestmp = $request->getTimeStmp();

$xml->save('tardo.xml');

?>
